package com.alog.senior.jdbc;

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @Auther: Ye Zhengdong
 * @Date: 2019/1/29 18:39
 * @Description:
 */
public class JdbcUtils {
    private static String URL ;
    private static String USER ;
    private static String PASSWORD ;
    private static String DRIVER ;
    static {
        try {
            Properties pro = new Properties();
            pro.load(JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
            URL = pro.getProperty("url");
            USER = pro.getProperty("user");
            PASSWORD = pro.getProperty("password");
            DRIVER = pro.getProperty("driver");
            Class.forName(DRIVER);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    /**
     * 获取连接
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }
    /**
     *  释放资源
     */
    public static void close(ResultSet rs, Statement smt, Connection conn){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(smt != null){
            try {
                smt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     *  释放资源
     */
    public static void close(Statement smt, Connection conn){
        if(smt != null){
            try {
                smt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
